package com.koron.css2.projectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.ProjectReceiptBean;
import com.koron.css2.newProjectCenter.mapper.ProjectReceiptMapper;
import com.koron.css2.projectCenter.bean.PjAddProjectBean;
import com.koron.css2.projectCenter.bean.PjConsultBean;
import com.koron.css2.projectCenter.bean.PjInstallBean;
import com.koron.css2.projectCenter.mapper.PjConsultMapper;
import com.koron.css2.projectCenter.mapper.PjInstallMapper;
import com.koron.css2.projectCenter.service.PjConsultService;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 用水安装追加工程收款
 * @author LGC
 *
 */
public class PjAddProject implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		try {
			PjAddProjectBean bean = JsonUtils.objectToPojo(req.getData(), PjAddProjectBean.class);
			//查询用水安装
			PjInstallMapper mapper = factory.getMapper(PjInstallMapper.class);
			PjInstallBean pjInstallBean = mapper.selectById(bean.getId());
			if (pjInstallBean == null) {
				return MessageBean.create(Constant.NOT_NULL, "用水安装单据不存在。", null);
			}
			
			//查询用水咨询
			PjConsultMapper consultMapper = factory.getMapper(PjConsultMapper.class);
			PjConsultBean pjConsultBean= consultMapper.selectById(pjInstallBean.getConsultId());
			if (pjConsultBean == null) {
				return MessageBean.create(Constant.NOT_NULL, "用水咨询单据不存在。", null);
			}
			pjConsultBean.setBudgetMoney(bean.getYsAmount());
			pjConsultBean.setDepositMoney(bean.getYjAmount());
			double accmoney = (bean.getYsAmount()==null?0.0:bean.getYsAmount())+(bean.getYjAmount()==null?0.0:bean.getYjAmount());
			addProjectReceipt(factory, userInfo, pjConsultBean, pjInstallBean,"3");
			pjInstallBean.setAccruedMoney(pjInstallBean.getAccruedMoney()==null?0.0:pjInstallBean.getAccruedMoney()+accmoney);
			//增加用水安装累计收款金额
			mapper.saveAccruedMoney(pjInstallBean);
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", null);
		} catch (Exception e) {
			factory.close(false);
			logger.error("用水安装保存失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
		}
	}
	public void addProjectReceipt(SessionFactory factory, UserInfoBean userInfo, PjConsultBean pjConsultBean, PjInstallBean pjInstallBean,String chargeType) {
		
		//组装工程收款单
		ProjectReceiptBean projectReceiptBean = new ProjectReceiptBean();
		projectReceiptBean.setId(Tools.getObjectId());
		projectReceiptBean.setBillId(pjInstallBean.getId());
		projectReceiptBean.setBillNo(pjInstallBean.getBillNo());
		projectReceiptBean.setBillDate(CommonUtils.getCurrentDate());
		projectReceiptBean.setBillType(PjConsultService.INSTALL_RECEIPT);
		projectReceiptBean.setReceivableMoney(pjConsultBean.getBudgetMoney());//应收
		projectReceiptBean.setDepositMoney(pjConsultBean.getDepositMoney());//押金
		double accmoney = (pjConsultBean.getBudgetMoney()==null?0.0:pjConsultBean.getBudgetMoney())+(pjConsultBean.getDepositMoney()==null?0.0:pjConsultBean.getDepositMoney());
		projectReceiptBean.setTotalMoney(accmoney);
		projectReceiptBean.setChargeFlag(0);
		projectReceiptBean.setComments("用水安装单据编号：" + pjInstallBean.getBillNo());
		projectReceiptBean.setTenantId(userInfo.getCurWaterCode());
		projectReceiptBean.setCreateAccount(userInfo.getUserInfo().getAcount());
		projectReceiptBean.setCreateName(userInfo.getUserInfo().getName());
		projectReceiptBean.setChargeType(chargeType);
		
		//新增工程收款单
		ProjectReceiptMapper projectReceiptMapper = factory.getMapper(ProjectReceiptMapper.class);
		projectReceiptMapper.add(projectReceiptBean);
	}
}
